//
//  UIButton+Saas.m
//  saas
//
//  Created by fangyibao on 2019/9/16.
//  Copyright © 2019 fangmao. All rights reserved.
//

#import "UIButton+Saas.h"
#import <objc/runtime.h>
NSString *const SaCountDownTimerKey = @"SaCountDownTimerKey";
@interface UIButton (Saas)
@property (nonatomic) dispatch_source_t timer;
@end
@implementation UIButton (Saas)

- (void)adjustButtonContentSpace:(CGFloat)space direction:(SaasButtonIamgeViewLacation)direction{
    [self setTitle:self.currentTitle forState:UIControlStateNormal];
    [self setImage:self.currentImage forState:UIControlStateNormal];
    
    CGFloat imageWidth  = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    CGFloat labelWidth  = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
    CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
#pragma clang diagnostic pop
    
    CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;
    CGFloat imageOffsetY = imageHeight / 2 + space / 2;
    CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;
    CGFloat labelOffsetY = labelHeight / 2 + space / 2;
    
    CGFloat tempWidth     = MAX(labelWidth, imageWidth);
    CGFloat changedWidth  = labelWidth + imageWidth - tempWidth;
    CGFloat tempHeight    = MAX(labelHeight, imageHeight);
    CGFloat changedHeight = labelHeight + imageHeight + space - tempHeight;
    
    switch (direction) {
        case SaasButtonImageViewLeft:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -space/2, 0, space/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, space/2, 0, -space/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, space/2, 0, space/2);
            break;
        case SaasButtonImageViewRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + space/2, 0, -(labelWidth + space/2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + space/2), 0, imageWidth + space/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, space/2, 0, space/2);
            break;
        case SaasButtonImageViewTop:
            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
            break;
        case SaasButtonImageViewBottom:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
            break;
        default:
            break;
    }
}
- (void)animationImageView:(NSString *)key duration:(CGFloat)duration{
    [UIView beginAnimations:key context:nil];
    [UIView setAnimationDuration:duration];
    self.imageView.transform = CGAffineTransformMakeRotation(self.isSelected?M_PI:2*M_PI);
    [UIView commitAnimations];
}
- (void)setCountDownTotalTime:(int)time attributeString:(BOOL)attributeString formate:(NSString *)formate highLightColor:(NSInteger)hC{
    __block int current = time;
    dispatch_queue_t queue = dispatch_get_main_queue();
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(self.timer, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), (uint64_t)(1.0 * NSEC_PER_SEC), 0);
    dispatch_source_set_event_handler(self.timer, ^{
        if (current<0){
            [self setAttributedTitle:[[NSAttributedString alloc] initWithString:@"重新发送"] forState:UIControlStateNormal];
            self.enabled = YES;
            dispatch_source_cancel(self.timer);
        }
        if (attributeString && current >= 0){
            [self setAttributedTitle:[self _countDownAttributeString:current colorHex:hC] forState:UIControlStateNormal];
            self.enabled = NO;
        }
        current --;
    });
    dispatch_resume(self.timer);
    
}
- (dispatch_source_t)timer{
    return objc_getAssociatedObject(self, &SaCountDownTimerKey);
}
- (void)setTimer:(dispatch_source_t)timer{
    objc_setAssociatedObject(self, &SaCountDownTimerKey, timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSAttributedString *)_countDownAttributeString:(int)currentTime colorHex:(NSInteger)colorH{
    NSString *time = [NSString stringWithFormat:@"%d", currentTime];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已发送%@s", time]];
//    [attr addAttributes:@{NSForegroundColorAttributeName:COLOR_HEX(colorH)} range:NSMakeRange(3, time.length)];
    [attr addAttributes:@{NSForegroundColorAttributeName:COLOR_RGB(50, 113, 237)} range:NSMakeRange(3, time.length)];
    return attr;
}
@end
